package com.nanhang.controller;

import com.nanhang.entity.TbSpecification;
import com.nanhang.service.TbSpecificationService;
import com.nanhang.util.PageResult;
import com.nanhang.util.R;
import com.nanhang.vo.TbSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tbspecification")
@CrossOrigin            // 跨域问题
public class TbSpecificationController {
  @Autowired
    TbSpecificationService tbSpecificationService;
  //查询所有的规格列表
    @GetMapping("/list")
    public List<TbSpecification> list(){
      return tbSpecificationService.findAll();
    }
    @GetMapping("/findByPage")
    public PageResult<TbSpecification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
      return tbSpecificationService.findByPage(page,pageSize);
    }
    //分页查找规格，其中page,pageSize为必须参数，规格对象为可选参数
  @PostMapping("/search")
  public PageResult<TbSpecification> search(@RequestParam("page") Integer page,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestBody(required = false)TbSpecificationVO tbSpecificationVO){
    System.out.println("hh");
      return tbSpecificationService.search(page,pageSize,tbSpecificationVO);
  }

  // 4. 保存规格
  @PostMapping("/save")
  public R save(@RequestBody TbSpecification tbSpecification){
    return tbSpecificationService.save(tbSpecification) ? R.ok().message("规格添加成功！") : R.error().message("规格添加失败！");
  }
}
